﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace SCCM_Client_Injector_2014
{
    public partial class LogViewerForm : Form
    {
        private static string LogDate;
        public LogViewerForm()
        {
            InitializeComponent();
            this.Text = "SCCM 2014 Client Injector Log Viewer - " + Properties.Settings.Default.LogFilePath;
        }

        private void LogViewerForm_Load(object sender, EventArgs e)
        {
            List<string> Log = FormattedLog();
            //MessageBox.Show("Log: " + Log);
            if (Log.Contains("FileNotFound") == true)
            {
            }
            else
            {
                string[] LogLine = Log.ToArray();

                foreach (string LogRecord in LogLine)
                {
                        string[] LogField = LogRecord.Split(new char[] { ',' });
                        if (LogField[0] != "")
                        {
                            ListViewItem row = new ListViewItem(LogField[0]);
                            row.SubItems.Add(new ListViewItem.ListViewSubItem(row, LogField[1]));
                            row.SubItems.Add(new ListViewItem.ListViewSubItem(row, LogField[2]));
                            row.SubItems.Add(new ListViewItem.ListViewSubItem(row, LogField[3]));
                            row.SubItems.Add(new ListViewItem.ListViewSubItem(row, LogField[5]));
                            row.SubItems.Add(new ListViewItem.ListViewSubItem(row, LogField[4]));
                            row.SubItems.Add(new ListViewItem.ListViewSubItem(row, LogField[6]));
                            listViewLogEntries.Items.Add(row);
                        }
                }
            }        
        }

        private List<string> FormattedLog()
        {
            List<string> Log = new List<string>();
             if (File.Exists(Properties.Settings.Default.LogFilePath) == true)
             {
                using (StreamReader reader = new StreamReader(Properties.Settings.Default.LogFilePath))
                {
                  string LogFile = reader.ReadToEnd();
                  string[] LogString = LogFile.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
                    int i = 0;
                    foreach (string Line in LogString)
                    {
                        if (i >= 5) { Log.Add(Line); }
                        i++;
                    }
                    reader.Close();
                }
             }
             else
             {
                 MessageBox.Show("Log File not found.  Please check that this file exists and is in the location shown in options.");
                 Log.Add("FileNotFound");
                 return(Log);
             }
             return (Log);
        }

        private void listViewLogEntries_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listViewLogEntries_MouseClick(object sender, MouseEventArgs e)
        {

        }
    }
}
